package cn.iocoder.yudao.module.hs.controller.admin.picktask;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.picktask.vo.PickTaskPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktask.vo.PickTaskRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktask.vo.PickTaskSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterial.vo.StartPickVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.picktask.PickTaskDO;
import cn.iocoder.yudao.module.hs.service.picktask.PickTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 拣货任务")
@RestController
@RequestMapping("/hs/pick-task")
@Validated
public class PickTaskController {

    @Resource
    private PickTaskService pickTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建拣货任务")
    @PreAuthorize("@ss.hasPermission('hs:pick-task:create')")
    public CommonResult<Long> createPickTask(@Valid @RequestBody PickTaskSaveReqVO createReqVO) {
        return success(pickTaskService.createPickTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拣货任务")
    @PreAuthorize("@ss.hasPermission('hs:pick-task:update')")
    public CommonResult<Boolean> updatePickTask(@Valid @RequestBody PickTaskSaveReqVO updateReqVO) {
        pickTaskService.updatePickTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拣货任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:pick-task:delete')")
    public CommonResult<Boolean> deletePickTask(@RequestParam("id") Long id) {
        pickTaskService.deletePickTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拣货任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:pick-task:query')")
    public CommonResult<PickTaskRespVO> getPickTask(@RequestParam("id") Long id) {
        PickTaskDO pickTask = pickTaskService.getPickTask(id);
        return success(BeanUtils.toBean(pickTask, PickTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拣货任务分页")
    @PreAuthorize("@ss.hasPermission('hs:pick-task:query')")
    public CommonResult<PageResult<PickTaskRespVO>> getPickTaskPage(@Valid PickTaskPageReqVO pageReqVO) {
        PageResult<PickTaskDO> pageResult = pickTaskService.getPickTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PickTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拣货任务 Excel")
    @PreAuthorize("@ss.hasPermission('hs:pick-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPickTaskExcel(@Valid PickTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PickTaskDO> list = pickTaskService.getPickTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拣货任务.xls", "数据", PickTaskRespVO.class,
                        BeanUtils.toBean(list, PickTaskRespVO.class));
    }

    @GetMapping("/start-pick")
    @Operation(summary = "获得拣货中待拣货list")
    @Parameter(name = "id", description = "拣货任务机型明细ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:query')")
    public CommonResult<StartPickVO> getStartPick(@RequestParam("pickTaskId") Long pickTaskId) {
        return success(pickTaskService.getStartPick(pickTaskId));
    }

    @PostMapping("/complete")
    @Operation(summary = "更新拣货任务状态为已完成")
    @PreAuthorize("@ss.hasPermission('hs:pick-task:update')")
    public CommonResult<Boolean> complete(@RequestParam("pickTaskId") Long pickTaskId) {
        pickTaskService.complete(pickTaskId);
        return success(true);
    }

    @PostMapping("/clear-picking-data")
    @Operation(summary = "清除指定任务的拣货中缓存")
    @PreAuthorize("@ss.hasPermission('hs:pick-task:update')")
    public CommonResult<Boolean> clearPickingData(@RequestParam("modeCode") String modeCode) {
        pickTaskService.clearPickingData(modeCode);
        return success(true);
    }
}